﻿using EasyCore.App;
using EasyCore.Auth;
using EasyCore.Authentications;
using EasyCore.Caching;
using EasyCore.Core.Builders;
using EasyCore.Core.Modules;
using EasyCore.DependencyInjection;
using EasyCore.Logging;
using EasyCore.ObjectMapper;
using EasyCore.Reflection;
using EasyCore.UnifyResult;
using EasyCore.UnitOfWork.Filters;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace EasyCore.Core
{
    public static class ModuleExtensions
    {
        /// <summary>
        /// 添加用户配置文件
        /// </summary>
        /// <returns></returns>
        public static WebApplicationBuilder AddJsonFiles(this WebApplicationBuilder builder) {
            //加载配置文件
            var executeDirectory = AppContext.BaseDirectory; // 获取程序执行目录
            var jsonFiles = Directory.GetFiles(executeDirectory, "*.json", SearchOption.TopDirectoryOnly).ToList(); //扫描的json文件
            if (jsonFiles.Any()) jsonFiles = jsonFiles.Where(x => !x.ToLower().StartsWith("appsettings.")).ToList();
            var envJsonFiles = jsonFiles.Where(x => x.ToLower().EndsWith($".{builder.Environment.EnvironmentName}.json".ToLower())).ToList();
            var otherJsonFiles = jsonFiles.Where(x => !x.ToLower().EndsWith(".development.json") && !x.ToLower().EndsWith(".production.json")
            && !x.ToLower().EndsWith("deps.json") && !x.ToLower().EndsWith("runtimeconfig.dev.json") && !x.ToLower().EndsWith("runtimeconfig.prod.json")
            && !x.ToLower().EndsWith(".runtimeconfig.json") && !x.ToLower().EndsWith("staticwebassets.runtime.json")).ToList();
            otherJsonFiles.AddRange(envJsonFiles); //合并json文件
            if (otherJsonFiles.Any())
            {
                foreach (var file in otherJsonFiles)
                {
                    builder.Configuration.AddJsonFile(file, optional: true, reloadOnChange: true);
                }
            }
            return builder;
        }
   

        /// <summary>
        /// 添加框架的中间件
        /// </summary>
        /// <param name="app"></param>
        /// <returns></returns>
        public static WebApplication UseEasyAdmin(this WebApplication app)
        {
            IServiceProvider provider = app.Services;
            try
            {
                var modules = provider.GetAllModules();
                foreach (EasyModule easyModule in modules)
                {
                    Type easyModuleType = easyModule.GetType();
                    string packName = easyModuleType.GetDescription();
                    easyModule.UseModule(provider);
                }
                return app;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// 获取所有模块信息
        /// </summary>
        public static EasyModule[] GetAllModules(this IServiceProvider provider)
        {
            EasyModule[] modules = provider.GetServices<EasyModule>().ToArray();
            return modules;
        }
    }
}
